﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.Account
{
    public partial class ChangeProfile : BasePage
    {
        private UserPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new UserPresenter();
            if(!IsPostBack)
            {
                PrepareView(); 
            }
        }

        protected void UpdateProfileButton_Click(object sender, EventArgs e)
        {
            DTOUser user= PopulateObject();
            if (_presenter.SetDataUser(user))
            {
                ShowMessage("Profile has been changed successfully.", "Success");
            }
            else
            {
                ShowMessage(LanguageLoader.GetText("FAILED_OPERATION_MESSAGE"), LanguageLoader.GetText("FAILED"));
            }
        }

        private void PrepareView()
        {
            DTOUser user = _presenter.GetUser(HttpContext.Current.User.Identity.Name);
            PopulateControls(user);    
        }

        public int Id
        {
            set { ViewState["Id"] = value; }
            get { return Convert.ToInt32( ViewState["Id"]); }
        }

        private void PopulateControls(DTOUser user)
        {
            UserName.Text = user.Name;
            LoginId.Text = user.LoginId;
            Email.Text = user.Email;
            SecretAnswer.Text = user.SecretAnswer;
            Id = user.Id;
        }
        private DTOUser PopulateObject()
        {
            DTOUser user = new DTOUser();
            user.Name = UserName.Text;
            user.LoginId = LoginId.Text;
            user.Email = Email.Text;
            user.SecretAnswer = SecretAnswer.Text;
            user.Id = Id;
            user.LastUpdated = DateTime.Now;
            return user;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResources();
        }

        private void SetLanguageAndResources()
        {
            NameLable.Text = LanguageLoader.GetText("NAME");
            LoginIdLable.Text = LanguageLoader.GetText("LOGIN_ID");
            EmailLabel.Text = LanguageLoader.GetText("EMAIL");
            SecretAnswerLable.Text = LanguageLoader.GetText("SECRET_ANSWER");
            UpdateProfileButton.Text = LanguageLoader.GetText("UPDATE_PROFILE");
        }
    }
}